package com.seuic.hnapps.project;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;
import com.seuic.hnapps.BaseActivity;
import com.seuic.hnapps.R;
import com.seuic.hnapps.bean.CardBinInfo_bean;
import com.seuic.hnapps.bean.StatisticalData_bean;
import com.seuic.hnapps.bean.Summary_Bean;
import com.seuic.hnapps.constants.EmvConstants;
import com.seuic.hnapps.posdsdk.pos_func.PosCallbacks;
import com.seuic.hnapps.posdsdk.pos_func.PosFuncs;
import com.seuic.hnapps.util.BRInterface;
import com.seuic.hnapps.util.CommAdapter;
import com.seuic.hnapps.util.Constants;
import com.seuic.hnapps.util.PinPad;
import com.seuic.hnapps.util.QBInterface;
import com.seuic.hnapps.util.ServiceCommon;
import com.seuic.hnapps.util.SpUtils;
import com.seuic.hnapps.util.TTTInterface;
import com.seuic.hnapps.util.Utils;
import com.seuic.hnapps.util.VTAInterface;
import com.solab.enc.DES3;
import com.solab.enc.HexT;
import com.solab.enc.SMS4;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.MessageFactory;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * isjingkai
 * 2019年7月20日10:47:53
 * 现金汇款
 */

public class CashRemittanceActivity extends BaseActivity {

    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.re_banknum_name)
    TextView reBanknumName;
    @BindView(R.id.re_banknum_num)
    TextView reBanknumNum;
    @BindView(R.id.re_banknum_okbtn)
    Button reBanknumOkbtn;
    @BindView(R.id.re_banknum_layout)
    LinearLayout reBanknumLayout;
    @BindView(R.id.re_selectbank_okbtn)
    Button reSelectbankOkbtn;
    @BindView(R.id.base_back_btn)
    LinearLayout baseBackBtn;
    @BindView(R.id.base_title_text)
    TextView baseTitleText;
    @BindView(R.id.re_select_ones_btn)
    Button reSelectOnesBtn;
    @BindView(R.id.re_select_others_btn)
    Button reSelectOthersBtn;
    @BindView(R.id.re_select_account_layout)
    LinearLayout reSelectAccountLayout;
    @BindView(R.id.re_input_handto_btn)
    LinearLayout reInputHandtoBtn;
    @BindView(R.id.re_input_read_btn)
    LinearLayout reInputReadBtn;
    @BindView(R.id.re_inputmoudle_layout)
    LinearLayout reInputmoudleLayout;
    @BindView(R.id.re_selectbank_btn)
    LinearLayout reSelectbankBtn;
    @BindView(R.id.re_selectbank_layout)
    LinearLayout reSelectbankLayout;
    @BindView(R.id.re_input_title)
    TextView reInputTitle;
    @BindView(R.id.re_input_gzbtn)
    Button reInputGzbtn;
    @BindView(R.id.re_input_okbtn)
    Button reInputOkbtn;
    @BindView(R.id.re_input_nameedtext)
    EditText reInputNameedtext;
    @BindView(R.id.re_input_numedtext)
    EditText reInputNumedtext;
    @BindView(R.id.re_input_namelayout)
    LinearLayout reInputNamelayout;
    @BindView(R.id.re_input_numgzbtn)
    Button reInputNumgzbtn;
    @BindView(R.id.re_input_numokbtn)
    Button reInputNumokbtn;
    @BindView(R.id.re_input_numlayout)
    LinearLayout reInputNumlayout;
    @BindView(R.id.re_input_namenumlayout)
    LinearLayout reInputNamenumlayout;
    @BindView(R.id.sure_printingpeper_okbtn)
    Button surePrintingpeperOkbtn;
    @BindView(R.id.sure_printingpeper_layout)
    LinearLayout surePrintingpeperLayout;
    @BindView(R.id.printing_layout)
    LinearLayout printingLayout;
    @BindView(R.id.inputnum_edtext)
    EditText inputnumEdtext;
    @BindView(R.id.input_gzbtn)
    Button inputGzbtn;
    @BindView(R.id.input_numokbtn)
    Button inputNumokbtn;
    @BindView(R.id.ficationing_layout)
    LinearLayout ficationingLayout;
    @BindView(R.id.input_money_edtext)
    EditText inputMoneyEdtext;
    @BindView(R.id.input_money_gzbtn)
    Button inputMoneyGzbtn;
    @BindView(R.id.input_money_okbtn)
    Button inputMoneyOkbtn;
    @BindView(R.id.input_money_layout)
    LinearLayout inputMoneyLayout;
    @BindView(R.id.sure_remittance_num)
    TextView sureRemittanceNum;
    @BindView(R.id.sure_remittance_money)
    TextView sureRemittanceMoney;
    @BindView(R.id.sure_remittance_namea)
    TextView sureRemittanceName;
    @BindView(R.id.sure_remittance_gzbtn)
    Button sureRemittanceGzbtn;
    @BindView(R.id.sure_remittance_okbtn)
    Button sureRemittanceOkbtn;
    @BindView(R.id.sure_remittance_layout)
    LinearLayout sureRemittanceLayout;
    @BindView(R.id.dealing_layout)
    LinearLayout dealingLayout;
    @BindView(R.id.remittance_success_moneynum)
    TextView remittanceSuccessMoneynum;
    @BindView(R.id.remittance_success_ok_btn)
    Button remittanceSuccessOkBtn;
    @BindView(R.id.remittance_success_layout)
    LinearLayout remittanceSuccessLayout;
    @BindView(R.id.input_num_layout)
    LinearLayout inputNumLayout;
    @BindView(R.id.please_input_iccard_layout)
    LinearLayout pleaseInputIccardLayout;
    @BindView(R.id.input_bankname_edtext)
    EditText inputBanknameEdtext;
    @BindView(R.id.input_bankname_okbtn)
    Button inputBanknameOkbtn;
    @BindView(R.id.input_bankname_nobtn)
    Button inputBanknameNobtn;
    @BindView(R.id.input_bankname_layout)
    LinearLayout inputBanknameLayout;
    @BindView(R.id.remittance_success_type)
    TextView remittanceSuccessType;
    @BindView(R.id.sure_remittance_title)
    TextView sureRemittanceTitle;


    private PosFuncs posFuncs;
    boolean ones_Bnak = true;//是否本行交易
    private String RemittanceNum;
    private String RemittanceName;
    private String RemittanceMoney;
    private ArrayList<String> banks = new ArrayList<>();
    OptionsPickerView optionsPickerView;
    Runnable runnable;
    Handler handler;
    private String bank_num = "666666";

    //输入方式
    String input_type;
    //卡类型
    String CardType;
    //二磁信息
    String track2Data;
    //三磁信息
    String track3Data;
    //ic卡55信息
    String Field55;
    //银行卡号
    String bindNum = "";
    private String pass_word = "";
    private String time;
    //流水号
    private int num;
    //银行号
    private String bind_num;
    //银行类型
    private String bank_type = "";
    //银行名
    private String bank_name;
    //操作类型 本行账户 还是他行账户
    private String make_type = "";
    //开户行机构号
    private String jgh;
    private String s_number;
    private String rwNUm;
    private String pz_nums;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cash_remittance);
        ButterKnife.bind(this);
        this.setBackBtClick(R.id.base_back_btn);
        this.setTitleText(R.id.base_title_text, "(   " + "现金汇款" + "   )");
        sureRemittanceTitle.setText("卡号");
        remittanceSuccessType.setText("当前客户的汇款金额为");
        posFuncs = PosFuncs.getInstace();
        posFuncs.init(this);
        bankName.setText("请选择银行");
        reInputTitle.setText("请输入转入人姓名");
        handler = new Handler();
        setPickViewData();
        initPackView();
    }


    @OnClick({
            R.id.re_select_ones_btn, R.id.re_select_others_btn,
            R.id.re_input_handto_btn, R.id.re_input_read_btn,
            R.id.re_selectbank_btn, R.id.re_banknum_okbtn,
            R.id.re_selectbank_okbtn, R.id.re_input_gzbtn,
            R.id.re_input_okbtn, R.id.re_input_numgzbtn,
            R.id.re_input_numokbtn, R.id.sure_printingpeper_okbtn,
            R.id.input_gzbtn, R.id.input_numokbtn,
            R.id.input_money_gzbtn,
            R.id.input_money_okbtn, R.id.sure_remittance_gzbtn,
            R.id.sure_remittance_okbtn, R.id.remittance_success_ok_btn,
            R.id.input_bankname_okbtn, R.id.input_bankname_nobtn
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.re_select_ones_btn:
                //点击选择了转本行账户
                reSelectAccountLayout.setVisibility(View.GONE);
                reInputmoudleLayout.setVisibility(View.VISIBLE);
                break;
//             <!---------------------------------本行--------------------------------->
//             <!---------------------------------本行--------------------------------->
//             <!---------------------------------本行--------------------------------->
//             <!---------------------------------本行--------------------------------->
//             <!---------------------------------本行--------------------------------->
            case R.id.re_input_handto_btn:
                //选择手输账号
                reInputmoudleLayout.setVisibility(View.GONE);
                inputNumLayout.setVisibility(View.VISIBLE);
                CardType = "input";
                break;
            case R.id.re_input_read_btn:
                //选择读取账户
                reInputmoudleLayout.setVisibility(View.GONE);
                pleaseInputIccardLayout.setVisibility(View.VISIBLE);

                pleaseInputIccardLayout.setVisibility(View.GONE);
                ficationingLayout.setVisibility(View.VISIBLE);
                readCardTest();

                break;
            case R.id.input_gzbtn:
                //输入账户 更正
                inputnumEdtext.setText("");
                break;
            case R.id.input_numokbtn:
                //输入账户 确定
                if (inputnumEdtext.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                } else {
                    if (inputnumEdtext.getText().toString().length() < 15 ||
                            inputnumEdtext.getText().toString().length() > 20) {
                        Toast.makeText(this, "请输入正确的银行卡号", Toast.LENGTH_SHORT).show();

                    } else {
                        bindNum = inputnumEdtext.getText().toString();
                        inputNumLayout.setVisibility(View.GONE);
                        ficationingLayout.setVisibility(View.VISIBLE);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                                Logger.e("第一次获取rz" + num + CardType);
                                excuteVTA(String.valueOf(num), bindNum, CardType, track2Data);
                                SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", num + 1);
                                Logger.e("第二次获取rz" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0));
                            }
                        }).start();
                    }
                }
                break;
            case R.id.input_money_gzbtn:
                //输入汇款金额更正
                inputMoneyEdtext.setText("");
                break;
            case R.id.input_money_okbtn:
                //输入汇款金额确定
                if (inputMoneyEdtext.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入汇款金额", Toast.LENGTH_SHORT).show();
                } else {

                    RemittanceMoney = inputMoneyEdtext.getText().toString();
//                    enterPin(Constants.DEMO_F102, Integer.parseInt(RemittanceMoney) * 100);
                    if (inputMoneyEdtext.getText().toString().contains(".")) {
                        DecimalFormat df = new DecimalFormat("######0.00");
                        String mons[] = inputMoneyEdtext.getText().toString().split("\\.");
                        if (mons[1].length() > 2) {
                            Toast.makeText(this, "请输入正确的金额", Toast.LENGTH_SHORT).show();
                        } else {
                            final Double d = Double.parseDouble(inputMoneyEdtext.getText().toString().trim());
                            df.format(d);
//                        enterPin(Constants.DEMO_F102, d * 100);
//                        tradeWithCards(d * 100);
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            inputMoneyLayout.setVisibility(View.GONE);
                                            sureRemittanceLayout.setVisibility(View.VISIBLE);
                                            sureRemittanceMoney.setText("￥" + d);
                                            if (make_type.equals("other")) {
                                                sureRemittanceNum.setText(reInputNumedtext.getText().toString());
                                                sureRemittanceName.setText(reInputNameedtext.getText().toString());
                                            } else {
                                                sureRemittanceNum.setText(bindNum);
                                                sureRemittanceName.setText(RemittanceName);
                                            }
                                        }
                                    });
                                }
                            }).start();
                        }

                    } else {
//                        enterPin(Constants.DEMO_F102, inputMoneyEdtext.getText().toString().trim()) * 100);
//                        tradeWithCards(Integer.parseInt(inputMoneyEdtext.getText().toString().trim()) * 100);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        inputMoneyLayout.setVisibility(View.GONE);
                                        sureRemittanceLayout.setVisibility(View.VISIBLE);
                                        sureRemittanceMoney.setText("￥" + Integer.parseInt(inputMoneyEdtext.getText().toString().trim()));
                                        if (make_type.equals("other")) {
                                            sureRemittanceNum.setText(reInputNumedtext.getText().toString());
                                            sureRemittanceName.setText(reInputNameedtext.getText().toString());
                                        } else {
                                            sureRemittanceNum.setText(bindNum);
                                            sureRemittanceName.setText(RemittanceName);
                                        }
                                    }
                                });
                            }
                        }).start();
                    }
                }
                break;
            case R.id.sure_remittance_gzbtn:
                //取消汇款信息
                if (ones_Bnak) {
                    inputnumEdtext.setText("");
                    inputMoneyEdtext.setText("");
                    sureRemittanceLayout.setVisibility(View.GONE);
                    reInputmoudleLayout.setVisibility(View.VISIBLE);
                } else {
                    bankName.setText("请选择银行");
                    reInputNamelayout.setVisibility(View.VISIBLE);
                    reInputNameedtext.setVisibility(View.VISIBLE);
                    reInputNumlayout.setVisibility(View.GONE);
                    reInputNumedtext.setVisibility(View.GONE);
                    reInputNameedtext.setText("");
                    reInputNumedtext.setText("");
                    inputMoneyEdtext.setText("");
                    reInputTitle.setText("请输入转入人姓名");
                    sureRemittanceLayout.setVisibility(View.GONE);
                    reSelectbankLayout.setVisibility(View.VISIBLE);
                }
                RemittanceNum = "";
                RemittanceMoney = "";
                RemittanceName = "";

                break;
            case R.id.sure_remittance_okbtn:
                //确认汇款信息
                sureRemittanceLayout.setVisibility(View.GONE);
                dealingLayout.setVisibility(View.VISIBLE);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// HH:mm:ss
                        //获取当前时间
                        Date date = new Date(System.currentTimeMillis());
                        time = simpleDateFormat.format(date);
                        if (make_type.equals("other")) {
                            int Num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                            Logger.e("第一次获取" + Num);
                            excuteBR(String.valueOf(Num), track2Data,CardType,reInputNumedtext.getText().toString(), pass_word, inputMoneyEdtext.getText().toString());
                            SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", Num + 1);
                            Logger.e("第二次获取" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 1000));

                        } else {

                            int Num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                            Logger.e("第一次获取" + Num);
                            excuteTTT(String.valueOf(Num), bindNum, CardType, track2Data, track3Data, pass_word, inputMoneyEdtext.getText().toString());
                            SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", Num + 1);
                            Logger.e("第二次获取" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 1000));

                        }

                    }
                }).start();
                break;
            case R.id.remittance_success_ok_btn:
                //汇款成功 确认
                remittanceSuccessLayout.setVisibility(View.GONE);
                surePrintingpeperLayout.setVisibility(View.VISIBLE);
                break;
//             <!---------------------------------他行--------------------------------->
//             <!---------------------------------他行--------------------------------->
//             <!---------------------------------他行--------------------------------->
//             <!---------------------------------他行--------------------------------->
//             <!---------------------------------他行--------------------------------->
            case R.id.re_select_others_btn:
                ones_Bnak = false;
                //点击选择了转他行账户
                make_type = "other";
                reSelectAccountLayout.setVisibility(View.GONE);
                reSelectbankLayout.setVisibility(View.VISIBLE);

                break;
            case R.id.re_selectbank_btn:
                //选择银行
                optionsPickerView.show();
                break;
            case R.id.re_selectbank_okbtn:
                //确定选择银行
                if (bank_type.equals("")) {
                    Toast.makeText(this, "请选择银行", Toast.LENGTH_SHORT).show();

                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                            Logger.e("第一次获取rz" + num + CardType);
                            excuteQB(String.valueOf(num));
                            SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", num + 1);
                            Logger.e("第二次获取rz" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0));
                        }
                    }).start();

                }
                break;
            case R.id.re_banknum_okbtn:
                //确定银行序号
                reBanknumLayout.setVisibility(View.GONE);
                reInputNamenumlayout.setVisibility(View.VISIBLE);
                break;
            case R.id.re_input_gzbtn:
                //输入姓名更正
                reInputNameedtext.setText("");
                break;
            case R.id.re_input_okbtn:
                //输入姓名确定
                if (reInputNameedtext.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
                } else {
                    RemittanceName = reInputNameedtext.getText().toString();
                    reInputTitle.setText("请输入汇款账号");
                    reInputNamelayout.setVisibility(View.GONE);
                    reInputNameedtext.setVisibility(View.GONE);
                    reInputNumlayout.setVisibility(View.VISIBLE);
                    reInputNumedtext.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.re_input_numgzbtn:
                //输入账号更正
                reInputNumedtext.setText("");
                break;
            case R.id.re_input_numokbtn:
                //输入账号确定
                if (reInputNumedtext.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                } else {
                    if (reInputNumedtext.getText().toString().length() < 15 || reInputNumedtext.getText().toString().length() > 19) {
                        Toast.makeText(this, "请输入正确的银行卡号", Toast.LENGTH_SHORT).show();
                    } else {
                        reInputNamenumlayout.setVisibility(View.GONE);
                        ficationingLayout.setVisibility(View.VISIBLE);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                                Logger.e("第一次获取rz" + num);
                                excuteVTA(String.valueOf(num), Constants.DEMO_F102, "", track2Data);
                                SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", num + 1);
                                Logger.e("第二次获取rz" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0));
                            }
                        }).start();
                    }
                }
                break;
            case R.id.sure_printingpeper_okbtn:
                //确定安装好打印纸
                surePrintingpeperLayout.setVisibility(View.GONE);
                printingLayout.setVisibility(View.VISIBLE);
                paint();
                break;
            case R.id.input_bankname_okbtn:
                //输入银行名称确定
                if (inputBanknameEdtext.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入银行名称", Toast.LENGTH_SHORT).show();
                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                            Logger.e("第一次获取rz" + num + CardType);
                            excuteQB(String.valueOf(num));
                            SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", num + 1);
                            Logger.e("第二次获取rz" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0));
                        }
                    }).start();
                }
                break;
            case R.id.input_bankname_nobtn:
                //输入银行名称更正
                inputBanknameEdtext.setText("");
                break;
        }
    }

    /**
     * 调取键盘
     */
    private void enterPin(final String bindNum, final double money) {
        Logger.e(money + "");
        new Thread() {
            @Override
            public void run() {
                posFuncs.enterPin(new PosCallbacks.EnterPinCallbacks() {
                    @Override
                    public void onSuccess(final String enc, boolean is_no_pin) {
                        if (is_no_pin) {

                        } else {
                            Log.e("", enc);
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    pass_word = enc;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            inputMoneyLayout.setVisibility(View.GONE);
                                            sureRemittanceLayout.setVisibility(View.VISIBLE);
                                            sureRemittanceMoney.setText("￥" + RemittanceMoney);
                                            if (make_type.equals("other")) {
                                                sureRemittanceNum.setText(reInputNumedtext.getText().toString());
                                                sureRemittanceName.setText(reInputNameedtext.getText().toString());
                                            } else {
                                                sureRemittanceNum.setText(bindNum);
                                                sureRemittanceName.setText(RemittanceName);
                                            }


                                        }
                                    });

//                                    int Num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
//                                    Logger.e("第一次获取" + Num);
////                                    excute(String.valueOf(Num), bindNum, track2Data, track3Data, importMoneynumEdtext.getText().toString(), enc, CardType, Field55);
//                                    SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", Num + 1);
//                                    Logger.e("第二次获取" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 1000));

                                }
                            }).start();

                        }
                    }

                    @Override
                    public void onFail(String error) {
                        Toast.makeText(CashRemittanceActivity.this, "交易结束", Toast.LENGTH_SHORT).show();
                        if (!error.equals(null)) {
                            finish();
                        }

                    }

                    @Override
                    public void onInput(int input_num) {
                        Log.e("", String.valueOf(input_num));
                    }
                }, bindNum, (int) money);
            }
        }.start();
    }

    /**
     * 读取卡
     */
    private void readCardTest() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                posFuncs.cardReaderConfig(true, true, true);

                posFuncs.emvSimpleFlow(new PosCallbacks.EmvTransStartCallbacks() {
                    @Override
                    public void onDetectCard(String type) {
                        CardType = type;
                        Log.e("", type);
                    }

                    @Override
                    public void onConfirmCardInfo(String pan) {
                        Logger.e("pan" + pan);
                        bindNum = pan;
                        if (bindNum.equals("")) {
                            Toast.makeText(CashRemittanceActivity.this, "请插入卡", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), pan, Toast.LENGTH_SHORT).show();
                            posFuncs.emvConfirmCardInfo(EmvConstants.CONFIRM_OK);

                        }
                    }

                    @Override
                    public void onOlinePinEntered(String enc, boolean is_no_pin) {

                    }

                    @Override
                    public void onSuccess(Bundle data) {
                        if (!data.isEmpty()) {
                            pleaseInputIccardLayout.setVisibility(View.GONE);
                            ficationingLayout.setVisibility(View.VISIBLE);

                            Log.e("", data.getString(EmvConstants.FEILD_55));
                            //如果是磁条卡，就获取二磁信息、上传给银行接口
                            // IC Card    MAG Card    RF Card
                            if (CardType.equals("MAG Card")) {
                                track2Data = data.getString(EmvConstants.TRACK2);
                                track3Data = data.getString(EmvConstants.TRACK3);

                            } else {
                                String Field55 = data.getString(EmvConstants.FEILD_55);
                                track2Data = Utils.formatField57(Utils.getValueFromIccData("57", Field55));
                                s_number = Utils.getValueFromIccData("5f34", Field55);
                            }
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    num = SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0);
                                    Logger.e("第一次获取rz" + num);
                                    excuteVTA(String.valueOf(num), bindNum, CardType, track2Data);
                                    SpUtils.getSPUtil(getApplicationContext()).putInt("s_Num", num + 1);
                                    Logger.e("第二次获取rz" + SpUtils.getSPUtil(getApplicationContext()).getInt("s_Num", 0));
                                }
                            }).start();
                        }

                    }

                    @Override
                    public void onFailed(String error) {
                        Toast.makeText(CashRemittanceActivity.this, error, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();


    }

    private void tradeWithCards(final double money) {
        PinPad.initPinPad(posFuncs);
        new Thread() {
            @Override
            public void run() {

                posFuncs.cardReaderConfig(true, true, true);
                posFuncs.emvTransStart(new PosCallbacks.EmvTransStartCallbacks() {
                    @Override
                    public void onDetectCard(String type) {

                    }

                    @Override
                    public void onConfirmCardInfo(final String pan) {

                        posFuncs.emvConfirmCardInfo(EmvConstants.CONFIRM_OK);

                    }

                    @Override
                    public void onOlinePinEntered(String enc, boolean is_no_pin) {
                        if (!is_no_pin) {
                            try {
                                String pin = PinPad.getPin(Constants.DEMO_F102, enc);
                                pass_word = pin;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }


                    }

                    @Override
                    public void onSuccess(Bundle data) {
                        String channel = data.getString(EmvConstants.CHANNEL);
                        if (EmvConstants.CHANNEL_MAGCARD.equals(channel)) {
//                            appendInfo("MagCard Used\n");
//                            appendInfo("Track1: " + data.getString(EmvConstants.TRACK1) + "\n");
//                            appendInfo("Track2: " + data.getString(EmvConstants.TRACK2) + "\n");
//                            appendInfo("Track3: " + data.getString(EmvConstants.TRACK3) + "\n");
//                            appendInfo("\nFinish\n");
//                            setEmvProcessStatus(false);
                        } else if (EmvConstants.CHANNEL_ICC.equals(channel)) {
//                            appendInfo("ICCard Used\n");

                            String ac_type = data.getString(EmvConstants.AC_TYPE);
                            Logger.e("ac_type" + ac_type);
                            Logger.e("Ac Type: " + data.getString(EmvConstants.AC_TYPE) + "\n");
                            Logger.e("Field 55: " + data.getString(EmvConstants.FEILD_55) + "\n");
                            String[] tags =
                                    {
                                            "9F26", "9F27", "9F10", "9F37", "9F36", "95",
                                            "9A", "9C", "9F02", "5F2A", "82", "9F1A", "9F03", "9F33", "9F34", "9F35",
                                            "9F1E", "84", "9F09", "9F41"
                                    };
                            Field55 = Utils.getField55(tags, data.getString(EmvConstants.FEILD_55));
                            Logger.e("f55" + Field55);
                            if (EmvConstants.AC_TYPE_ARQC.equals(ac_type)) {

                                Bundle bundle = new Bundle();
                                bundle.putString(EmvConstants.RESPONSE_CODE, "3030");
                                posFuncs.emvOnlineResult(new PosCallbacks.EmvOnlineResultCallbacks() {
                                    @Override
                                    public void onSuccess(Bundle data) {
                                        Logger.e("Ac Type: " + data.getString(EmvConstants.AC_TYPE) + "\n");
                                        Logger.e("Field 55: " + data.getString(EmvConstants.FEILD_55) + "\n");

                                    }

                                    @Override
                                    public void onFailed(String error) {

                                    }
                                }, bundle);
                            } else {

                            }
                        } else if (EmvConstants.CHANNEL_ICCL.equals(channel)) {
                            Logger.e("Ac Type: " + data.getString(EmvConstants.AC_TYPE) + "\n");
                            Logger.e("Field 55: " + data.getString(EmvConstants.FEILD_55) + "\n");

                        }
                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        inputMoneyLayout.setVisibility(View.GONE);
                                        sureRemittanceLayout.setVisibility(View.VISIBLE);
                                        sureRemittanceMoney.setText("￥" + RemittanceMoney);
                                        if (make_type.equals("other")) {
                                            sureRemittanceNum.setText(reInputNumedtext.getText().toString());
                                            sureRemittanceName.setText(reInputNameedtext.getText().toString());
                                        } else {
                                            sureRemittanceNum.setText(bindNum);
                                            sureRemittanceName.setText(RemittanceName);
                                        }


                                    }
                                });
                            }
                        }).start();
                    }

                    @Override
                    public void onFailed(String error) {

                    }
                }, (int) money, 0);
            }
        }.start();
    }


    private void paint() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                posFuncs.printerClearBuffer();
                posFuncs.printerAppendLargeTextAlignCenter("烟台银行 通用凭证");

                posFuncs.printerAppendTextline("交易时间 :" + time);
                posFuncs.printerAppendTextline("设备号 :" + Constants.YANTAI_TERMNO);
                posFuncs.printerAppendTextline("操作员 :" + SpUtils.getSPUtil(getApplicationContext()).getString("make_user", ""));
                if (make_type.equals("other")) {
                    posFuncs.printerAppendTextline("转入账号 :" + reInputNumedtext.getText().toString());
                } else {
                    posFuncs.printerAppendTextline("账号/卡号 :" + bindNum);
                }

                posFuncs.printerAppendTextline("流水号 :" + rwNUm);
                if (make_type.equals("other")) {
                    posFuncs.printerAppendTextline("业务类型 :" + "他行现金汇款");
                    posFuncs.printerAppendTextline("户名 :" + reInputNameedtext.getText().toString());
                } else {
                    posFuncs.printerAppendTextline("业务类型 :" + "本行现金汇款");
                    posFuncs.printerAppendTextline("户名 :" + RemittanceName);
                }
                if (RemittanceMoney.contains(".")) {
                    String moneys[] = RemittanceMoney.split("\\.");
                    if (moneys[1].length() == 1) {
                        String monses = moneys[0] + "." + moneys[1] + "0";
                        posFuncs.printerAppendTextline("交易金额 :" + monses);
                    } else {
                        posFuncs.printerAppendTextline("交易金额 :" + RemittanceMoney);
                    }

                } else {
                    posFuncs.printerAppendTextline("交易金额 :" + RemittanceMoney + ".00");
                }

                posFuncs.printerAppendTextline("凭证序号 :" + pz_nums);
                posFuncs.printerAppendTextline("客户签字确认 :");
                posFuncs.printerAppendTextline("--------------------------------------");
                posFuncs.printerAppendLargeTextAlignCenter("烟台银行 通用凭证");
                posFuncs.printerAppendTextline("交易时间 :" + time);
                posFuncs.printerAppendTextline("设备号 :" + Constants.YANTAI_TERMNO);
                posFuncs.printerAppendTextline("操作员 :" + SpUtils.getSPUtil(getApplicationContext()).getString("make_user", ""));
                if (make_type.equals("other")) {
                    String bindx = reInputNumedtext.getText().toString().substring(6, reInputNumedtext.length() - 5);
                    String bindxs = "*";
                    for (int i = 0; i < bindx.length(); i++) {
                        bindxs = bindxs + "*";
                    }
                    posFuncs.printerAppendTextline("转入账号 :" + reInputNumedtext.getText().toString().substring(0, 6) + bindxs + reInputNumedtext.getText().toString().substring(reInputNumedtext.getText().toString().length() - 4, reInputNumedtext.getText().toString().length()));
                } else {
                    String bindx = bindNum.substring(6, bindNum.length() - 5);
                    String bindxs = "*";
                    for (int i = 0; i < bindx.length(); i++) {
                        bindxs = bindxs + "*";
                    }
                    posFuncs.printerAppendTextline("账号/卡号 :" + bindNum.substring(0, 6) + bindxs + bindNum.substring(bindNum.length() - 4, bindNum.length()));
                }
                posFuncs.printerAppendTextline("流水号 :" + rwNUm);
                if (make_type.equals("other")) {
                    posFuncs.printerAppendTextline("业务类型 :" + "他行现金汇款");
                    String RemittanceNameone = reInputNameedtext.getText().toString().substring(0, 1);
                    posFuncs.printerAppendTextline("户名 :" + reInputNameedtext.getText().toString().replaceFirst(RemittanceNameone, "*"));
                } else {
                    posFuncs.printerAppendTextline("业务类型 :" + "本行现金汇款");
                    String RemittanceNameone = RemittanceName.substring(0, 1);
                    posFuncs.printerAppendTextline("户名 :" + RemittanceName.replaceFirst(RemittanceNameone, "*"));
                }

                if (RemittanceMoney.contains(".")) {
                    String moneys[] = RemittanceMoney.split("\\.");
                    if (moneys[1].length() == 1) {
                        String monses = moneys[0] + "." + moneys[1] + "0";
                        posFuncs.printerAppendTextline("交易金额 :" + monses);
                    } else {
                        posFuncs.printerAppendTextline("交易金额 :" + RemittanceMoney);
                    }

                } else {
                    posFuncs.printerAppendTextline("交易金额 :" + RemittanceMoney + ".00");
                }
                posFuncs.printerAppendTextline("凭证序号 :" + pz_nums);
                posFuncs.printerAppendTextline("客户签字确认 :");
                posFuncs.printerAppendTextline("");
                posFuncs.printerAppendTextline("");
                posFuncs.printerAppendTextline("");
                posFuncs.printerAppendTextline("");
                posFuncs.printerAppendTextline("");
                posFuncs.printerAppendTextline("");
                posFuncs.printerAppendTextline("");


                posFuncs.startPrint(new PosCallbacks.PrintCallbacaks() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(CashRemittanceActivity.this, "打印成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailed(String error) {
                        if (error.equals("Lack Paper")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CashRemittanceActivity.this, "请确认打印纸安装正确", Toast.LENGTH_SHORT).show();
                                    surePrintingpeperLayout.setVisibility(View.VISIBLE);
                                    printingLayout.setVisibility(View.GONE);
                                }
                            });
                        }
                    }
                });
            }
        }.start();
    }

    //初始化packView
    private void initPackView() {
        optionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                bank_type = (banks.get(options1));
                Toast.makeText(CashRemittanceActivity.this, banks.get(options1), Toast.LENGTH_SHORT).show();
                bankName.setText(banks.get(options1));
                if (bank_type.equals("其他")) {
                    reSelectbankLayout.setVisibility(View.GONE);
                    inputBanknameLayout.setVisibility(View.VISIBLE);
                }
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
            }
        })
                .setSelectOptions(0)
                .build();
        optionsPickerView.setPicker(banks);
    }

    private void setPickViewData() {
        banks.add("中国工商银行");
        banks.add("中国农业银行");
        banks.add("中国银行");
        banks.add("中国建设银行");
        banks.add("交通银行");
        banks.add("招商银行");
        banks.add("其他");
    }

    /**
     * 银行行名行号查询
     */
    public void excuteQB(String transn) {

        IsoMessage msg = null;
        QBInterface inter = new QBInterface();
        msg = inter.packRequest(transn, bank_type, inputBanknameEdtext.getText().toString());
        excuteRequest(msg, "qb");
    }

    /**
     * 账户验证
     */
    public void excuteVTA(String transn, String bindNum, String CardTYpe, String track2Data) {
        String c_bin_info = SpUtils.getSPUtil(getApplicationContext()).getString("c_bin_info", "null");
        Gson gson = new Gson();
        List<CardBinInfo_bean> beans = gson.fromJson(c_bin_info, new TypeToken<List<CardBinInfo_bean>>() {
        }.getType());
        IsoMessage msg = null;
        VTAInterface inter = new VTAInterface();
        msg = inter.packRequest(bindNum, beans, transn, track2Data, CardTYpe, String.valueOf(2), s_number);
        excuteRequest(msg, "zhrz");
    }

    /**
     * 本行账户现金汇款
     * 本转本
     *
     * @param
     * @param
     */
    public void excuteTTT(String transn, String bindNum, String CardTYpe, String track2Data, String track3Data, String password, String money) {
       Logger.e("bzb");
        String c_bin_info = SpUtils.getSPUtil(getApplicationContext()).getString("c_bin_info", "null");
        Gson gson = new Gson();
        List<CardBinInfo_bean> beans = gson.fromJson(c_bin_info, new TypeToken<List<CardBinInfo_bean>>() {
        }.getType());
        IsoMessage msg = null;
        TTTInterface inter = new TTTInterface();
        msg = inter.packRequest(bindNum, transn, money, CardTYpe, track2Data, track3Data, password, s_number, Field55);
        excuteRequest(msg, "bzb");
    }

    /**
     * 本行账户现金汇款
     * 本转他
     *
     * @param
     * @param
     */
    public void excuteBR(String transn,String track2Data,String CardTYpe, String bindNum, String password, String money) {

        IsoMessage msg = null;
        BRInterface inter = new BRInterface();
        msg = inter.packRequest(bindNum, transn, money, password, bind_num,
                reInputNameedtext.getText().toString(), jgh, RemittanceName);
        excuteRequest(msg, "bzt");
    }


    private void excuteRequest(IsoMessage msg, final String op_type) {

        byte[] data = msg.writeData();
        // ��д����ͷ��7�򳤶�ֵ
        int hlen = Constants.ATMP_HEADER_LENGTH;
        int bodylen = data.length - hlen;
        String headF7 = ServiceCommon.appendZero(bodylen, 4);
        System.arraycopy(headF7.getBytes(), 0, data, 28, 4);
        // ����MAC
        if (msg.getField(128) != null) {
//            int start = hlen;
//            int length = data.length - start - 8;
//            byte[] inmac = new byte[length];
//            byte[] outmac = new byte[16];
//            System.arraycopy(data, start, inmac, 0, length);
//            Logger.e("MAC����=[{}]" + HexT.byteArr2HexStr(inmac));
//            Logger.e(Constants.YANTAI_MACKEY);
//            String mackey = Constants.YANTAI_MACKEY;
//            // MAC99�㷨makֻ��Ҫ16�ֽ���Կ
//            if (mackey.length() > 16) {
//                mackey = mackey.substring(0, 16);
//            }
//            DES3.getMacBlock_99(inmac, mackey.getBytes(), outmac);
//            System.arraycopy(outmac, 0, data, data.length - 8, 8);
//            Logger.e("����MAC=[{}]" + new String(outmac).substring(0, 8));
            int start = hlen;
            int length = data.length - start - 8;
            byte[] inmac = new byte[length];
            byte[] outmac = new byte[16];
            System.arraycopy(data, start, inmac, 0, length);
            System.out.println("MAC数据=[" + HexT.byteArr2HexStr(inmac) + "]");
            String mackey = Constants.YANTAI_MACKEY;

            if (Constants.isSM) {
                // SM4 MAC919算法
                byte[] mkey = HexT.hexstr2ByteArr(mackey);
                String macval = SMS4.sms4MacBlock_919(inmac, mkey);
                outmac = macval.getBytes();
            } else {
                // MAC99算法mak只需要16字节密钥
                if (mackey.length() > 16) {
                    mackey = mackey.substring(0, 16);
                }
                DES3.getMacBlock_99(inmac, mackey.getBytes(), outmac);
            }

            System.arraycopy(outmac, 0, data, data.length - 8, 8);
            System.out.println("请求MAC=[" + new String(outmac).substring(0, 8) + "]");
        }
        byte[] send = null;
        // ���ļ��ܣ���������ʱ��ҵ���ױ��뱨�ļ���
        String encflag = msg.getHeadStringValue(4);
        if ("1".equals(encflag)) {
            String tdk = Constants.YANTAI_TDKKEY;
            int lenr = 8 - bodylen % 8;
            byte[] head = new byte[hlen];
            byte[] decdata = new byte[bodylen + lenr];
            System.arraycopy(data, 0, head, 0, hlen);
            System.arraycopy(data, hlen, decdata, 0, bodylen);
            byte[] encdata = DES3.des3Packet(decdata, tdk.getBytes());
            send = new byte[hlen + encdata.length];
            System.arraycopy(head, 0, send, 0, hlen);
            System.arraycopy(encdata, 0, send, hlen, encdata.length);

        } else if ("3".equals(encflag)) {
            String tdk = Constants.YANTAI_TDKKEY;
            byte[] tdkey = HexT.hexstr2ByteArr(tdk);
            byte[] h7 = new byte[4];
            System.arraycopy(data, 28, h7, 0, 4);
            int orilen = Integer.parseInt(new String(h7));
            int enclen = data.length - Constants.ATMP_HEADER_LENGTH;
            byte[] head = new byte[Constants.ATMP_HEADER_LENGTH];
            byte[] encdata = new byte[enclen];
            System.arraycopy(data, 0, head, 0, Constants.ATMP_HEADER_LENGTH);
            System.arraycopy(data, Constants.ATMP_HEADER_LENGTH, encdata, 0, enclen);
            byte[] decdata = SMS4.unsms4Packet(encdata, tdkey);

            send = new byte[Constants.ATMP_HEADER_LENGTH + orilen];
            System.arraycopy(head, 0, send, 0, Constants.ATMP_HEADER_LENGTH);
            System.arraycopy(decdata, 0, send, Constants.ATMP_HEADER_LENGTH, orilen);
        } else {
            send = data;
        }

        CommAdapter adapter = new CommAdapter(Constants.IP, 3551);
        adapter.setHeadlen(4); // ����ͷ��4�ֽ�
        byte[] recv = adapter.doComm(send);

        byte[] recv2 = null;
        byte[] b = new byte[1];
        System.arraycopy(recv, 11, b, 0, 1);
        String rspenc = new String(b);
        Logger.e("Ӧ���ļ��ܱ�־:[{}]" + rspenc);
        if ("1".equals(rspenc)) {
            String tdk = Constants.YANTAI_TDKKEY;
            byte[] h7 = new byte[4];
            System.arraycopy(recv, 28, h7, 0, 4);
            int orilen = Integer.parseInt(new String(h7));
            int enclen = recv.length - Constants.ATMP_HEADER_LENGTH;
            byte[] head = new byte[Constants.ATMP_HEADER_LENGTH];
            byte[] encdata = new byte[enclen];
            System.arraycopy(recv, 0, head, 0, Constants.ATMP_HEADER_LENGTH);
            System.arraycopy(recv, Constants.ATMP_HEADER_LENGTH, encdata, 0, enclen);
            byte[] decdata = DES3.undes3Packet(encdata, tdk.getBytes());

            recv2 = new byte[Constants.ATMP_HEADER_LENGTH + orilen];
            System.arraycopy(head, 0, recv2, 0, Constants.ATMP_HEADER_LENGTH);
            System.arraycopy(decdata, 0, recv2, Constants.ATMP_HEADER_LENGTH, orilen);
        } else {
            recv2 = recv;
        }

        IsoMessage rsp = null;
        MessageFactory<IsoMessage> fac = new MessageFactory<IsoMessage>();
        try {
            rsp = fac.parseMessage(recv2, 32);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String respcode = rsp.getStringValue(39);
        if ("00".equals(respcode)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (op_type.equals("zhrz")) {
                        Toast.makeText(getApplicationContext(), "验证成功", Toast.LENGTH_SHORT).show();
                    } else if (op_type.equals("bzb")) {

                    }
                }
            });
            unpackReSpone(rsp, op_type);
        } else if ("96".equals(respcode)) {

            Gson gson = new Gson();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");// HH:mm:ss
            //获取当前时间
            Date date = new Date(System.currentTimeMillis());
            if (op_type.equals("bzb")) {
                Logger.e("Date获取当前日期时间" + simpleDateFormat.format(date).replace("/", "") + "UN_DATA");
                StatisticalData_bean bean = new StatisticalData_bean(rwNUm, inputMoneyEdtext.getText().toString(), time, "本转本现金汇款", bindNum, RemittanceName, SpUtils.getSPUtil(getApplicationContext()).getString("make_user", ""), String.valueOf(pz_nums));
                String STAT_DATA = SpUtils.getSPUtil(getApplicationContext()).getString(simpleDateFormat.format(date).replace("/", ""), "");

                List<StatisticalData_bean> beans = gson.fromJson(STAT_DATA, new TypeToken<List<StatisticalData_bean>>() {
                }.getType());
                beans.add(bean);
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "UN_DATA", gson.toJson(beans));
                finish();
            } else {
                Logger.e("Date获取当前日期时间" + simpleDateFormat.format(date).replace("/", "") + "UN_DATA");
                StatisticalData_bean bean = new StatisticalData_bean(rwNUm, inputMoneyEdtext.getText().toString(), time, "本转他现金汇款", Constants.DEMO_F102, RemittanceName, SpUtils.getSPUtil(getApplicationContext()).getString("make_user", ""), String.valueOf(pz_nums));
                String STAT_DATA = SpUtils.getSPUtil(getApplicationContext()).getString(simpleDateFormat.format(date).replace("/", ""), "");

                List<StatisticalData_bean> beans = gson.fromJson(STAT_DATA, new TypeToken<List<StatisticalData_bean>>() {
                }.getType());
                beans.add(bean);
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "UN_DATA", gson.toJson(beans));
                finish();
            }


        } else {
            Logger.e("����ʧ��:[{}]-[{}]" + respcode + rsp.getStringValue(40));
            final IsoMessage finalRsp = rsp;
            CashRemittanceActivity.this.runOnUiThread(new Runnable() {
                @SuppressLint("WrongConstant")
                @Override
                public void run() {
                    if (finalRsp.getStringValue(40).equals("该功能尚不支持")) {
                        Toast.makeText(getApplicationContext(), "卡号错误", 3000).show();
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(), finalRsp.getStringValue(40), Toast.LENGTH_LONG);
                        showMyToast(toast, 5 * 1000);
                    }
                }
            });

            this.finish();
        }
    }

    public void showMyToast(final Toast toast, final int cnt) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt);
    }


    private void unpackReSpone(IsoMessage response, String op_type) {
        rwNUm = response.getStringValue(37);
        if (op_type.equals("zhrz")) {
            SpUtils.getSPUtil(getApplicationContext()).putString("all_make_type", "zhrz");
            String f123 = response.getStringValue(123);

            String f123s[] = f123.split("\\|");
            RemittanceName = f123s[2];
            jgh = f123s[0];

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    handler = new Handler();
                    runnable = new Runnable() {
                        @Override
                        public void run() {
                            ficationingLayout.setVisibility(View.GONE);
                            inputMoneyLayout.setVisibility(View.VISIBLE);
                        }
                    };
                    handler.postDelayed(runnable, 1000);

                }
            });
        } else if (op_type.equals("bzb")) {
            SpUtils.getSPUtil(getApplicationContext()).putString("all_make_type", "bzb");
            int pz_num = SpUtils.getSPUtil(getBaseContext()).getInt("pz_Num", 0);
            if (pz_num < 10) {
                pz_nums = "000" + pz_num;
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            } else if (pz_num >= 10 && pz_num < 100) {
                pz_nums = "00" + pz_num;
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            } else if (pz_num >= 100 && pz_num < 1000) {
                pz_nums = "0" + pz_num;
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            } else if (pz_num >= 1000 && pz_num < 10000) {
                pz_nums = String.valueOf(pz_num);
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            }


            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dealingLayout.setVisibility(View.GONE);
                    remittanceSuccessLayout.setVisibility(View.VISIBLE);
                    remittanceSuccessMoneynum.setText("￥" + RemittanceMoney);
                }
            });
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");// HH:mm:ss
            //获取当前时间
            Date date = new Date(System.currentTimeMillis());
            Logger.e("Date获取当前日期时间" + simpleDateFormat.format(date).replace("/", ""));
            StatisticalData_bean bean = new StatisticalData_bean(rwNUm, inputMoneyEdtext.getText().toString(), time, "本转本现金汇款", bindNum, RemittanceName, SpUtils.getSPUtil(getApplicationContext()).getString("make_user", ""), String.valueOf(pz_nums));
            String STAT_DATA = SpUtils.getSPUtil(getApplicationContext()).getString(simpleDateFormat.format(date).replace("/", ""), "");
            Gson gson = new Gson();
            List<StatisticalData_bean> beans = gson.fromJson(STAT_DATA, new TypeToken<List<StatisticalData_bean>>() {
            }.getType());
            beans.add(bean);
            SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", ""), gson.toJson(beans));

//汇总查询
            String STAT_DATAALL = SpUtils.getSPUtil(getApplicationContext()).getString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", "");
            List<Summary_Bean> beanalls = gson.fromJson(STAT_DATAALL, new TypeToken<List<Summary_Bean>>() {
            }.getType());
            if (!beanalls.get(0).getXjhk().equals("")) {
                String data[] = beanalls.get(0).getXjhk().split(",");
                int num = Integer.valueOf(data[1]) + 1;
                double money;

                Logger.e(data[0] + "aaaaaaaaaaa" + inputMoneyEdtext.getText().toString());
                if (data[0].contains(".") && inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Double.parseDouble(data[0]) + Double.parseDouble(inputMoneyEdtext.getText().toString());
                } else if (data[0].contains(".") && !inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Double.parseDouble(data[0]) + Integer.parseInt(inputMoneyEdtext.getText().toString());
                } else if (!data[0].contains(".") && inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Integer.valueOf(data[0]) + Double.parseDouble(inputMoneyEdtext.getText().toString());
                } else {
                    money = Integer.valueOf(data[0]) + Integer.valueOf(inputMoneyEdtext.getText().toString());
                }
//                BigDecimal n = new BigDecimal("0");

                Logger.e(Double.parseDouble(data[0]) + "bbbbbbbbbbbbbb");
                String Xjhk = money + "," + num;
                Summary_Bean summary_bean = new Summary_Bean(beanalls.get(0).getZnqk(), Xjhk, beanalls.get(0).getZzhk(), beanalls.get(0).getYdhf(), beanalls.get(0).getLthf(), beanalls.get(0).getSgcc(), beanalls.get(0).getDzh(), beanalls.get(0).getHzd(), beanalls.get(0).getZhgm(), beanalls.get(0).getLwhc(), beanalls.get(0).getKtgs(), "", "", beanalls.get(0).getZhcqy(), beanalls.get(0).getZhcjy(), beanalls.get(0).getHnyqy(), beanalls.get(0).getHnyjy());
                beanalls.clear();
                beanalls.add(summary_bean);
                Logger.e(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA");
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));

            } else {
                int num = 1;
                double money;
                if (inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Double.parseDouble(inputMoneyEdtext.getText().toString());
                    String Xjhk = money + "," + num;
                    Summary_Bean summary_bean = new Summary_Bean(beanalls.get(0).getZnqk(), Xjhk, beanalls.get(0).getZzhk(), beanalls.get(0).getYdhf(), beanalls.get(0).getLthf(), beanalls.get(0).getSgcc(), beanalls.get(0).getDzh(), beanalls.get(0).getHzd(), beanalls.get(0).getZhgm(), beanalls.get(0).getLwhc(), beanalls.get(0).getKtgs(), "", "", beanalls.get(0).getZhcqy(), beanalls.get(0).getZhcjy(), beanalls.get(0).getHnyqy(), beanalls.get(0).getHnyjy());
                    beanalls.clear();
                    beanalls.add(summary_bean);
                    Logger.e(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA");
                    SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));

                } else {
                    money = Integer.valueOf(inputMoneyEdtext.getText().toString());
                    String Xjhk = money + "," + num;
                    Summary_Bean summary_bean = new Summary_Bean(beanalls.get(0).getZnqk(), Xjhk, beanalls.get(0).getZzhk(), beanalls.get(0).getYdhf(),  beanalls.get(0).getLthf(),  beanalls.get(0).getSgcc(), beanalls.get(0).getDzh(),  beanalls.get(0).getHzd(), beanalls.get(0).getZhgm(), beanalls.get(0).getLwhc(), beanalls.get(0).getKtgs(), "", "", beanalls.get(0).getZhcqy(), beanalls.get(0).getZhcjy(), beanalls.get(0).getHnyqy(), beanalls.get(0).getHnyjy());
                    beanalls.clear();
                    beanalls.add(summary_bean);
                    Logger.e(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA");
                    SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));

                }


            }

        } else if (op_type.equals("qb")) {
            SpUtils.getSPUtil(getApplicationContext()).putString("all_make_type", "qb");
            Logger.e(response.getStringValue(123));
            final String f123s[] = response.getStringValue(123).split("\\|");
            bind_num = f123s[0].substring(2, f123s[0].length());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    reSelectbankLayout.setVisibility(View.GONE);
                    reBanknumLayout.setVisibility(View.VISIBLE);
                    reBanknumName.setText(f123s[1]);
                    reBanknumNum.setText("序号: " + bind_num);
                }
            });

        } else if (op_type.equals("bzt")) {
            SpUtils.getSPUtil(getApplicationContext()).putString("all_make_type", "bzt");
            int pz_num = SpUtils.getSPUtil(getBaseContext()).getInt("pz_Num", 0);
            if (pz_num < 10) {
                pz_nums = "000" + pz_num;
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            } else if (pz_num >= 10 && pz_num < 100) {
                pz_nums = "00" + pz_num;
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            } else if (pz_num >= 100 && pz_num < 1000) {
                pz_nums = "0" + pz_num;
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            } else if (pz_num >= 1000 && pz_num < 10000) {
                pz_nums = String.valueOf(pz_num);
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", pz_num + 1);
            }


            Gson gson = new Gson();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");// HH:mm:ss
            //获取当前时间
            Date date = new Date(System.currentTimeMillis());

//正常交易
            StatisticalData_bean bean = new StatisticalData_bean(rwNUm, inputMoneyEdtext.getText().toString(), time, "本转他现金汇款", Constants.DEMO_F102, RemittanceName, SpUtils.getSPUtil(getApplicationContext()).getString("make_user", ""), String.valueOf(pz_nums));
            String STAT_DATA = SpUtils.getSPUtil(getApplicationContext()).getString(simpleDateFormat.format(date).replace("/", ""), "");

            List<StatisticalData_bean> beans = gson.fromJson(STAT_DATA, new TypeToken<List<StatisticalData_bean>>() {
            }.getType());
            beans.add(bean);
            SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", ""), gson.toJson(beans));


            //汇总查询
            String STAT_DATAALL = SpUtils.getSPUtil(getApplicationContext()).getString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", "");
            List<Summary_Bean> beanalls = gson.fromJson(STAT_DATAALL, new TypeToken<List<Summary_Bean>>() {
            }.getType());
            if (!beanalls.get(0).getXjhk().equals("")) {
                String data[] = beanalls.get(0).getXjhk().split(",");

                int num = Integer.valueOf(data[1]) + 1;
                double money;

                Logger.e(data[0] + "aaaaaaaaaaa" + inputMoneyEdtext.getText().toString());
                if (data[0].contains(".") && inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Double.parseDouble(data[0]) + Double.parseDouble(inputMoneyEdtext.getText().toString());
                } else if (data[0].contains(".") && !inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Double.parseDouble(data[0]) + Integer.parseInt(inputMoneyEdtext.getText().toString());
                } else if (!data[0].contains(".") && inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Integer.valueOf(data[0]) + Double.parseDouble(inputMoneyEdtext.getText().toString());
                } else {
                    money = Integer.valueOf(data[0]) + Integer.valueOf(inputMoneyEdtext.getText().toString());
                }
//                BigDecimal n = new BigDecimal("0");

                Logger.e(Double.parseDouble(data[0]) + "bbbbbbbbbbbbbb");


                String Xjhk = money + "," + num;
                Logger.e(Xjhk + "Xjhk");
                Summary_Bean summary_bean = new Summary_Bean(beanalls.get(0).getZnqk(), Xjhk, beanalls.get(0).getZzhk(), beanalls.get(0).getYdhf(), beanalls.get(0).getLthf(), beanalls.get(0).getSgcc(), beanalls.get(0).getDzh(), beanalls.get(0).getHzd(), beanalls.get(0).getZhgm(), beanalls.get(0).getLwhc(), beanalls.get(0).getKtgs(), "", "", beanalls.get(0).getZhcqy(), beanalls.get(0).getZhcjy(), beanalls.get(0).getHnyqy(), beanalls.get(0).getHnyjy());
                beanalls.clear();
                beanalls.add(summary_bean);
                Logger.e(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA");
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));

            } else {
                int num = 1;
                double money;
                if (inputMoneyEdtext.getText().toString().contains(".")) {
                    money = Double.parseDouble(inputMoneyEdtext.getText().toString());
                    String Xjhk = money + "," + num;
                    Summary_Bean summary_bean = new Summary_Bean(beanalls.get(0).getZnqk(), Xjhk, beanalls.get(0).getZzhk(), beanalls.get(0).getYdhf(), beanalls.get(0).getLthf(), beanalls.get(0).getSgcc(), beanalls.get(0).getDzh(), beanalls.get(0).getHzd(), beanalls.get(0).getZhgm(), beanalls.get(0).getLwhc(), beanalls.get(0).getKtgs(), "", "", beanalls.get(0).getZhcqy(), beanalls.get(0).getZhcjy(), beanalls.get(0).getHnyqy(), beanalls.get(0).getHnyjy());
                    beanalls.clear();
                    beanalls.add(summary_bean);
                    Logger.e(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA");
                    SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));
                } else {
                    money = Integer.valueOf(inputMoneyEdtext.getText().toString());
                    String Xjhk = money + "," + num;
                    Summary_Bean summary_bean = new Summary_Bean(beanalls.get(0).getZnqk(), Xjhk, beanalls.get(0).getZzhk(), beanalls.get(0).getYdhf(), beanalls.get(0).getLthf(), beanalls.get(0).getSgcc(), beanalls.get(0).getDzh(), beanalls.get(0).getHzd(), beanalls.get(0).getZhgm(), beanalls.get(0).getLwhc(), beanalls.get(0).getKtgs(), "", "", beanalls.get(0).getZhcqy(), beanalls.get(0).getZhcjy(), beanalls.get(0).getHnyqy(), beanalls.get(0).getHnyjy());
                    beanalls.clear();
                    beanalls.add(summary_bean);
                    Logger.e(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA");
                    SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));
                }
            }


            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dealingLayout.setVisibility(View.GONE);
                    remittanceSuccessLayout.setVisibility(View.VISIBLE);
                    remittanceSuccessMoneynum.setText("￥" + RemittanceMoney);
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
    }

    @Override
    public void finish() {
        super.finish();
    }


}
